<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.rulesId" clearable class="filter-item" style="width: 200px;" placeholder="请输入规则编号" />
      <el-button v-permission="['POST /admin/group-buying/list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button v-permission="['POST /admin/group-buying/create']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <!-- <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出 -->
      <!-- </el-button> -->
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" label="团购规则ID" prop="rulesId" />

      <el-table-column align="center" label="商品ID" prop="goodsId" />

      <el-table-column align="center" min-width="100" label="名称" prop="goodsName" />

      <!-- <el-table-column align="center" label="团购优惠" prop="discount" /> -->

      <el-table-column align="center" label="团购要求" prop="discountMember" />

      <el-table-column align="center" label="参与人数" prop="boudaryMember" />

      <el-table-column align="center" label="状态" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 0 ? 'success' : 'error' ">{{ statusMap[scope.row.status] }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="结束时间" prop="expireTime" />

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-permission="['POST /admin/groupon/delete']" type="danger" size="mini" @click="handleDelete(scope.row)">下线</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :current.sync="listQuery.current" :size.sync="listQuery.size" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="120px" style="width: 400px; margin-left:50px;">
        <el-form-item label="规则id" prop="rulesId">
          <el-input v-model.number="dataForm.rulesId" />
        </el-form-item>
        <el-form-item label="团购活动数目" prop="count">
          <el-input v-model.number="dataForm.count" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
      </div>
    </el-dialog>

    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>

  </div>
</template>

<style>
.table-expand {
    font-size: 0;
}

.table-expand label {
    width: 100px;
    color: #99a9bf;
}

.table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
}

.gallery {
    width: 80px;
    margin-right: 10px;
}
</style>

<script>
import { createActivity, listAllActivityByPage, deleteActivity } from '@/api/groupon'
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'GrouponActivity',
  components: { BackToTop, Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        current: 1,
        size: 20,
        rulesId: '',
        orderBy: 'add_time',
        sort: 'asc'
      },
      textMap: {
        update: '编辑',
        create: '创建'
      },
      statusMap: [
        '团购上线',
        '人数满足',
        '团购超时',
        '团购下线'
      ],
      dataForm: {
        id: undefined,
        count: undefined
      },
      rules: {
        id: [{ required: true, message: '规则id不能为空', trigger: 'blur' }],
        count: [{ required: true, message: '团购活动数目不能为空', trigger: 'blur' }]
      },
      dialogStatus: '',
      goodsDetail: '',
      dialogFormVisible: false,
      detailDialogVisible: false,
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listAllActivityByPage(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        count: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(row) {
      deleteActivity(row)
        .then(response => {
          this.$notify.success({
            title: '成功',
            message: '删除团购活动成功'
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createActivity(this.dataForm)
            .then(response => {
              // this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '创建团购活动成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.errmsg
              })
            })
        }
      })
    }
  }
}
</script>
